/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.model;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.api.IWhereResult;
import net.sourceforge.perforce.core.resources.IP4File;
import net.sourceforge.perforce.ui.PerforceUIPlugin;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

/**
  * @version $Revision: 171 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class P4File implements IP4File, IAdaptable
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the changelist id. */
	private Integer changelistId;
	/** Stores the action. */
	private String action;
	/** Stores the depot filename. */
	private String depotFilename;
	/** Stores the revision. */
	private Integer revision;
	/** Stores the client name. */
	private String clientName;
	/** Stores the server. */
	private IPerforceServer server;
	/** Stores the 'our lock' indicator. */
	private boolean ourLock;
	/** Stores the 'resolved' indicator. */
	private boolean resolved = true;
		
	// -------------------------------------------------------------------------------- Constructors

	/** Constructor for P4File.
	  *
	  * @param theServer the perforce server.
	  */
	P4File(IPerforceServer theServer)
	{
		server = theServer;
	}

	// -------------------------------------------------------------------- Public Methods (IP4File)
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#getChangelistId() */
	public Integer getChangelistId() 
	{
		return changelistId;
	}
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#getAction() */
	public String getAction() 
	{
		return action;
	}

	/** @see net.sourceforge.perforce.core.resources.IP4File#getDepotFilename() */
	public String getDepotFilename() 
	{
		return depotFilename;
	}
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#getLocation() */
	public IPath getLocation() 
	{
		try
		{
			IWhereResult whereResult = server.where(depotFilename);
			return new Path(whereResult.getLocalFilename());
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
			return Path.EMPTY;
		}
	}

	/** @see net.sourceforge.perforce.core.resources.IP4File#getHaveRev() */
	public Integer getRevision() 
	{
		return revision;
	}

	/** @see net.sourceforge.perforce.core.resources.IP4File#getName() */
	public String getName() 
	{
		int pos = depotFilename.lastIndexOf('/') + 1;
		return depotFilename.substring(pos);
	}
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#getClientName() */
	public String getClientName() 
	{
		return clientName;
	}
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#getServer() */
	public IPerforceServer getServer() 
	{
		return server;
	}
	
	/** @see net.sourceforge.perforce.core.resources.IP4File#hasOurLock() */
	public boolean hasOurLock() 
	{
		return ourLock;
	}

	/** @see net.sourceforge.perforce.core.resources.IP4File#isResolved() */
	public boolean isResolved() 
	{
		return resolved;
	}
	
	// ----------------------------------------------------------------- Public Methods (IAdaptable)
	
	/** @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) */
	public Object getAdapter(Class adapter) 
	{
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}	
	
	// ------------------------------------------------------------------------------ Public Methods

	/** Sets the changelist id.
	  *
	  * @param newChangelistId the new changelist id. 
	  */
	public void setChangelistId(Integer newChangelistId)
	{
		changelistId = newChangelistId;
	}

	/** Sets the action.
	  *
	  * @param newAction the new action. 
	  */	
	public void setAction(String newAction)
	{
		action = newAction;
	}
	
	/** Sets the depot filename.
	  * 
	  * @param newDepotFilename the new depot filename. 
	  */
	public void setDepotFilename(String newDepotFilename)
	{
		depotFilename = newDepotFilename;
	}
	
	/** Sets the have revision.
	  *
	  * @param newHaveRev the new have revision. 
	  */
	public void setRevision(Integer newHaveRev)
	{
		revision = newHaveRev;
	}

	/** Sets the client name.
	  *
	  * @param newClientName the new client name. 
	  */
	public void setClientName(String newClientName)
	{
		clientName = newClientName;
	}
	
	/** Sets the our lock value.
	  *
	  * @param newOurLock the lock indicator value. 
	  */
	public void setOurLock(boolean newOurLock)
	{
		ourLock = newOurLock;
	}
	
	/** Sets the resolved value.
	  *
	  * @param newResolved the new resolved value. 
	  */
	public void setResolved(boolean newResolved)
	{
		resolved = newResolved;
	}
}
